class Solution122 {
    public int maxProfit(int[] prices) {
        int dp0 = 0, dp1 = -prices[0];
        for (int i = 1; i < prices.length; ++i) {
            int newDp0 = Math.max(dp0, dp1 + prices[i]);
            dp1 = Math.max(dp1, dp0 - prices[i]);
            dp0 = newDp0;
        }
        return dp0;
    }

    public int maxProfit1(int[] prices) {
        int ans = 0;
        int n = prices.length;
        for (int i = 1; i < n; ++i) {
            ans += Math.max(0, prices[i] - prices[i - 1]);
        }
        return ans;
    }
}

/* 作者：力扣官方题解
链接：https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/solutions/476791/mai-mai-gu-piao-de-zui-jia-shi-ji-ii-by-leetcode-s/
来源：力扣（LeetCode）
著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。 */